package org.jeecg.modules.purchase.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.exception.ServiceException;
import org.jeecg.modules.purchase.entity.PurchaseGoods;
import org.jeecg.modules.purchase.entity.PurchaseStock;
import org.jeecg.modules.purchase.mapper.PurchaseGoodsMapper;
import org.jeecg.modules.purchase.service.IPurchaseGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 物品
 * @Author: jeecg-boot
 * @Date:   2022-11-14
 * @Version: V1.0
 */
@Service
public class PurchaseGoodsServiceImpl extends ServiceImpl<PurchaseGoodsMapper, PurchaseGoods> implements IPurchaseGoodsService {

    @Autowired
    private PurchaseStockServiceImpl purchaseStockService;
    @Override
    public void saveAndValidation(PurchaseGoods purchaseGoods) {
        this.save(purchaseGoods);
    }

    @Override
    public void updateByIdAndValidation(PurchaseGoods purchaseGoods) {
        LambdaQueryWrapper<PurchaseGoods> queryWrapper = new LambdaQueryWrapper<>();
        //根据物品编码查询数据库拿到更改之前的物品信息
        String code = purchaseGoods.getCode();
        queryWrapper.eq(PurchaseGoods::getCode,code);
        //拿到更改前的物品信息  根据物品编码  名称  成本价来确定库存中是否有这个物品
        PurchaseGoods beforeGoods = this.getOne(queryWrapper);
        LambdaQueryWrapper<PurchaseStock> wrapper =  new LambdaQueryWrapper<>();
        wrapper.eq(PurchaseStock::getGoodsName,beforeGoods.getGoodsName())
                .eq(PurchaseStock::getGoodsCode,beforeGoods.getCode())
                .eq(PurchaseStock::getGoodsUnitPrice,beforeGoods.getUnitPrice());
        List<PurchaseStock> stockList = purchaseStockService.list(wrapper);
        //有则更改这个物品的信息
        if(ObjectUtil.isNotEmpty(stockList) && stockList.size()>0)
        {
            for (PurchaseStock stock : stockList) {
                stock.setGoodsTypeCode(purchaseGoods.getGoodsTypeCode());
                stock.setGoodsName(purchaseGoods.getGoodsName());
                stock.setGoodsUnit(purchaseGoods.getUnit());
                stock.setBrand(purchaseGoods.getBrand());
                stock.setGoodsSize(purchaseGoods.getSize());
                stock.setGoodsUnitPrice(new BigDecimal(Double.valueOf(purchaseGoods.getUnitPrice())));
                stock.setSellingPrice(new BigDecimal(Double.valueOf(purchaseGoods.getSellingPrice())));
                purchaseStockService.updateById(stock);
            }
        }
        //最后更改物品信息
        this.updateById(purchaseGoods);
    }

    @Override
    public void remove(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        //查询库存中是否有这个物品
        List<String> goodsCode = this.listByIds(idList).stream().map(PurchaseGoods::getCode).collect(Collectors.toList());
        LambdaQueryWrapper<PurchaseStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PurchaseStock::getGoodsCode,goodsCode);
        List<PurchaseStock> stockList = purchaseStockService.list(queryWrapper);
        //删除库存中的物品
        if(ObjectUtil.isNotEmpty(stockList) && stockList.size()>0){
            purchaseStockService.removeBatchByIds(stockList.stream().map(PurchaseStock::getId).collect(Collectors.toList()));
        }
        this.removeByIds(idList);
    }
}
